package com.hqyj.javaSpringBoot.modules.test.controller;

import com.hqyj.javaSpringBoot.modules.common.entiy.ResultEntity;
import com.hqyj.javaSpringBoot.modules.common.entiy.SearchBean;
import com.hqyj.javaSpringBoot.modules.test.entity.Student;
import com.hqyj.javaSpringBoot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 添加学生信息
     * 127.0.0.1/api/student   ---post
     * {"studentName":"张三","age":18,"sex":"male"}
     * {"studentName":"李四","age":18,"sex":"male","studentCard":{"cardNo":"123"}}
     * @param student 学生对象
     * @return result结果集
     */
    @PostMapping(value = "/student",consumes = "application/json")
    public ResultEntity<Student> insertStudent(@RequestBody Student student){
        return studentService.insertStudent(student);
    }

    /**
     * 删除学生信息
     * 127.0.0.1/api/student/4   ---delete
     * @param id 学生id
     * @return result结果集
     */
    @DeleteMapping(value = "/student/{id}")
    public ResultEntity<Student> deleteStudent(@PathVariable int id){
        return studentService.deleteStudent(id);
    }

    /**
     * 修改学生信息
     * 127.0.0.1/api/student   ---put
     * {"id":1,"studentName":"张三1","age":18,"sex":"male"}
     * @param student 学生id
     * @return result结果集
     */
    @PutMapping(value = "/student",consumes = "application/json")
    public ResultEntity<Student> updateStudent(@RequestBody Student student){
        return studentService.updateStudent(student);
    }

    /**
     * 修改学生信息
     * 127.0.0.1/api/student   ---put
     * {"id":1,"studentName":"张三1","age":18,"sex":"male"}
     * @param student 学生id
     * @return result结果集
     */
    @PutMapping(value = "/studentV2",consumes = "application/json")
    public ResultEntity<Student> updateStudentAgeById(@RequestBody Student student){
        return studentService.updateStudentNameById(student);
    }


    /**
     * 根据id查询学生信息
     * 127.0.0.1/api/student/1   ---get
     * @param id 学生id
     * @return student
     */
    @GetMapping(value = "student/{id}")
    public Student getStudentById(@PathVariable int id){
        return studentService.getStudentById(id);
    }


    /**
     * 根据id查询学生信息
     * hql语句查询
     * 127.0.0.1/api/studentV2/1   ---get
     * @param id 学生id
     * @return student
     */
    @GetMapping(value = "studentV2/{id}")
    public Student getStudentByIdV2(@PathVariable int id){
        return studentService.getStudentById(id);
    }

    /**
     * 根据学生姓名和年龄查询数据
     * 127.0.0.1/api/studentV3?studentName=张三111&age=18   ---get
     * @param studentName 学生姓名
     * @param age   年龄
     * @return  student
     */
    @GetMapping(value = "/studentV3")
    public Student getStudentNameAndAge(@RequestParam String studentName,@RequestParam Integer age){
        return studentService.getStudentNameAndAge(studentName,age);
    }


    /**
     * 根据学生姓名模糊查询且分页查询
     * 127.0.0.1/api/students  ---post
     *{"currentPage":1, "pageSize":5, "orderBy":"studentName",
     *       "direction":"desc", "keyWord": "张"}
     * @param searchBean 学生姓名
     * @return   Page<Student>
     */
    @PostMapping(value = "/students",consumes = "application/json")
    public Page<Student> getStudentBySearch(@RequestBody SearchBean searchBean){
        return studentService.getStudentBySearch(searchBean);
    }
}
